package com.sgx.learning.luban.data_structure;

import java.util.Scanner;

/**
 * 深度优先DFS
 */
public class DFS {

    private int n;      //表示行
    private int m;      //表示列

    private int dx;     //表示当前位置的x坐标
    private int dy;     //表示当前位置的y坐标

    private int minStep = Integer.MAX_VALUE;

    private int data[][];
    private boolean mark[][];

    public DFS(int n, int m, int dx, int dy, int[][] data, boolean[][] mark) {
        this.n = n;
        this.m = m;
        this.dx = dx;
        this.dy = dy;
        this.data = data;
        this.mark = mark;
    }

    public void dfs(int x,int y,int step) {
        //方向向量
        int next[][] = {{0,1},{1,0},{0,-1},{-1,0}};
        if(x == dx && y == dy) {
            if(step < minStep) {
                minStep = step;
            }
            return ;
        }
        for (int i = 0; i < 4; i++) {
            int nextX = x + next[i][0];
            int nextY = y  + next[i][1];
            if(nextX < 1 || nextX > n || nextY < 1 || nextY > m) {
            } else {
                if(!mark[nextX][nextY] && data[nextX][nextY] == 0) {
                    mark[nextX][nextY] = true;
                    dfs(nextX,nextY,step + 1);
                    mark[nextX][nextY] = false;
                }
            }
        }
    }

    /**
     *     0 0 1 0
     *     0 0 0 0
     *     0 0 1 0
     *     0 1 0 0
     *     0 0 0 1
     */
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        int m = cin.nextInt();

        int data[][] = new int[n+1][m+1];
        for (int i = 1; i <= n ; i++) {
            for (int j = 1; j <= m ; j++) {
                data[i][j] = cin.nextInt();
            }
        }
        int dx = cin.nextInt();
        int dy = cin.nextInt();
        boolean mark[][] = new boolean[n+1][m+1];
        DFS dfs = new DFS(n,m,dx,dy,data,mark);
        int startX = cin.nextInt();
        int startY = cin.nextInt();
        dfs.dfs(startX,startY,0);
        System.out.println(dfs.minStep);
    }
}
